package com.heima.minio.test;

import com.heima.file.service.impl.MinIOFileStorageService;
import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.FileInputStream;
import java.io.FileNotFoundException;

/**
 * @Author: xrl
 * @Date: 2022/10/12  22:51
 * @Version 1.0
 * 封装MinIO为starter 测试
 */
@SpringBootTest
public class MinIOTest2 {

    @Autowired
    private MinIOFileStorageService minIOFileStorageService;

    /**
     * 上传文件
     */
    @Test
    void testUpload() throws FileNotFoundException {
        FileInputStream is = new FileInputStream("E:\\code_plus\\index2.html");
        String path = minIOFileStorageService.uploadHtmlFile("", "index.html", is);
        System.out.println(path);
    }

    //上传插件到minio
    @Test
    void testMinIo1() {
        try {
            //读取本地文件
            FileInputStream fis = new FileInputStream("E:\\java课件\\新黑马头条课件\\day02-app端文章查看，静态化freemarker," +
                    "分布式文件系统minIO\\资料\\文章详情-" +
                    "模板文件\\plugins\\css\\index.css");
            //创建minion客户端
            MinioClient client = MinioClient.builder()
                    .credentials("minio", "minio123")//minion登录名和密码
                    .endpoint("http://192.168.200.130:9000")//minion连接地址
                    .build();

            //上传文件
            PutObjectArgs putObjectArgs = PutObjectArgs.builder()
                    .object("plugins/css/index.css") //文件名称
                    .bucket("qincun66") //minion上的桶名称
                    .contentType("text/css")//文件类型
                    .stream(fis, fis.available(), -1).build();
            client.putObject(putObjectArgs);

            //访问地址
//            System.out.println("http://192.168.200.130:9000/qincun/index.html");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 删除文件
     */
    @Test
    void testDelete() {
        minIOFileStorageService.delete("http://192.168.200.130:9000/qincun66/2022/10/12/index.html");
    }
}
